/*
* $Id: timer.c,v 1.3 2007/11/27 04:04:13 sunny Exp $
*/
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: func.c

       Version: 0.2.3

       Usage: oter function: Led, Timer...

       Platform: U-NET01 DK with Keil 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124, C8051F340

               UBEC: UZ2400, UZ2410

       Note : LED 4~7 Only for U-zig interface

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/
#if defined(__K0__)
#pragma section @@DATA @@TIMER
//#pragma inline  // enable memcpy() and memset() with direct inline expansion instead of function call
#endif // #if __K0__

#include "unet.h"

#define CLOCK_SECOND 1000
/* 38400 cycles @ 2.4576MHz with divisor 8 ==> 1/8 s */
/* #define INTERVAL (307200ULL / CLOCK_SECOND) */
#define INTERVAL (4096ULL / CLOCK_SECOND)

__interrupt void timerA1(void);

/*******************************************************************************

	Timer Function

********************************************************************************/

void init_1ms_timer(void)
{
	InitTimerVars();

	/* Select ACLK 32768Hz clock, divide by 8 */
	TACTL = TASSEL_1 | TACLR | ID_3;

	/* Initialize ccr1 to create the X ms interval. */
	/* CCR1 interrupt enabled, interrupt occurs when timer equals CCR1. */
	TACCTL1 = CCIE;

	/* Interrupt after X ms. */
	TACCR1 = INTERVAL;

	/* Start Timer_A in continuous mode. */
	TACTL |= MC_2;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Name:	timerA1()
 * Purpose:	timer A1 interrupt service function
 * Input:	none, this is an interrupt trap
 * Function:	executes once each timer 0 interrupt, maintains counters.
 * Output:	none
 *
 */
#pragma vector=TIMERA1_VECTOR
__interrupt void timerA1(void)
{
	if(TAIV == 2) {
		/* Make sure interrupt time is future */
		do
		{
			TACCR1 += INTERVAL;
		//	++count;
		} while ((TACCR1 - TAR) > INTERVAL);
	//	last_tar = TAR;
	}

	UpdateTimerVars();

}	/* end of Timer0() */
